package handlers

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
	"news-hubs/models"
)

func Add(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		title := c.PostForm("title")
		content := c.PostForm("content")
		art := models.Art{
			Title:   title,
			Content: content,
		}
		err := db.Create(&art).Error
		if err != nil {
			c.JSON(200, gin.H{
				"code": 0,
				"msg":  "添加失败",
			})
			return
		} else {
			c.JSON(200, gin.H{
				"code": 200,
				"msg":  "发布成功",
			})
			return
		}
	}
}
func List(db *gorm.DB) gin.HandlerFunc {
	return func(c *gin.Context) {
		var art []models.Art
		err := db.Find(&art).Error
		if err != nil {
			c.JSON(200, gin.H{
				"code": 0,
				"msg":  "查询失败",
			})
			return
		} else {
			c.JSON(200, gin.H{
				"code": 200,
				"msg":  "查询成功",
				"data": art,
			})
			return
		}
	}
}
